﻿using System;
using System.Security.Cryptography;
using Alipay.AopSdk.Core.Domain;
using Alipay.AopSdk.Core.Util;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;


namespace FwProject.WXMiddleware
{
  public  class WxChartOptions
    {
        /// <summary>
		///  应用ID,您的APPID
		/// </summary>
		public string AppId { get; set; }

        /// <summary>
        /// 合作商户uid
        /// </summary>
        public string Mch_id { get; set; }

        /// <summary>
        /// 微信统一下单网关网关
        /// </summary>
        public string Gatewayurl { get; set; } = "https://api.mch.weixin.qq.com/pay/unifiedorder";

        /// <summary>
        /// 商户私钥，您的原始格式RSA私钥
        /// </summary>
        public string PrivateKey { get; set; }

    

        /// <summary>
        /// 签名方式
        /// </summary>
        public string SignType { get; set; } = "RSA2";

        /// <summary>
  

        /// <summary>
        /// 是否从文件读取公私钥 如果为true ，那么公私钥应该配置为密钥文件路径
        /// </summary>
        public bool IsKeyFromFile { get; set; } = false;

        public void SetOption(IConfigurationSection section)
        {
            if (section == null)
            {
                throw new ArgumentException(nameof(section));
            }

            var options = section.Get<WxChartOptions>();
            SetOption(options);
        }

        public void SetOption(WxChartOptions options)
        {
            if (options == null)
            {
                throw new ArgumentException(nameof(options));
            }

            //SignType私钥检查
            if (string.IsNullOrEmpty(options.SignType))
            {
                throw new Exception("您的检查，详细信息：签名类型未指定！");
            }

            //RSA私钥检查
            if (string.IsNullOrEmpty(options.PrivateKey))
            {
                throw new Exception("您的微信key！");
            }

           
            this.AppId = options.AppId;
            this.Mch_id = options.Mch_id;
            this.Gatewayurl = options.Gatewayurl;
            this.PrivateKey = options.PrivateKey;
            this.SignType = options.SignType;
        
        }

    }

}
